<?php
/*
* 推广会员注册
* 暂时不支持推广码操作(2014-05-29 支持推广码)
*/
class PregisterBehavior extends Behavior
{

    protected $options   =  array('REGISTER_ON'=>true);
    protected $numtostr = array('a'=>0,'r'=>1,'c'=>2,'k'=>3,'e'=>4,'w'=>5,'u'=>6,'h'=>7,'n'=>8,'m'=>9);
    private $JDDB = '';
    
    public function run(&$params=array())
	{	
		$this->JDDB = C('JD_DB');
		if($this->options['REGISTER_ON'])
		{
			//推广码
			$preadcode = session('spreadcode');
			if(strlen($preadcode)==7){
				//前台用户推广的
				return $this->pop_from_member($preadcode,$params);
			}else{
				//后台用户推广的
				return $this->pop_from_osuser($preadcode,$params);
			}
		}
    }

    //前台用户推广的
    private function pop_from_member($preadcode,&$params=array()){
    	$prememberid=  '';
    	$prememberarr = str_split($preadcode);
    	foreach($prememberarr as $val){
    		$prememberid .= $this->numtostr[$val];
    	}
    	if(strlen($prememberid)!=7){
    		return false;
    	}
    	$real_premeberid = intval($prememberid-1000000);
    	$member_info = M("{$this->JDDB}member")->where(array('id'=>$real_premeberid,'password'=>array('neq','')))->find();
    	if(!$member_info) return;
    	$addarr = array(
    		'memberid' => $member_info['id'],
    		'pop_memberid' => $params['id'],
    		'pop_time'		=> $params['timeadd'],
    		'pop_key'		=> $preadcode,
    	);
    	$res = M('popularize_member')->add($addarr);
    	return $res?true:false;
    }
    //后台用户推广的
    private function pop_from_osuser($preadcode,&$params=array()){
    		if(empty($params['mobile'])) return;
			$Model = M("{$this->JDDB}popularizeMember");
    		$preaduserid = '';
			if($preadcode){
				$preadcodearr = str_split($preadcode);
				$realcode = '';
				foreach($preadcodearr as $val){
					$realcode .= $this->numtostr[$val];
				}
				$proeaduserinfo = M("{$this->JDDB}popularize_user")->where(array('code'=>$realcode))->find();
				if($proeaduserinfo){
					$preaduserid = $proeaduserinfo['userid'];
				}
			}
			$exists = $Model->where(array('mobile'=>$params['mobile']))->find();
			if(!$exists){
				if($preaduserid){
					//不存在手机号并有有效推广码 =>添加一条新记录
					$addarr = array(
						'userid' => $preaduserid,
						'memberid' => $params['id'],
						'name'		=> '推广用户@'.$preadcode,
						'mobile'	=> $params['mobile'],
						'timeadd'	=> $params['timeadd'],
						'registertime' => $params['timeadd'],
						'status'	=> 1,
					);
					$res = $Model->add($addarr);
					return $res?true:fasle;
				}else {
					//不存在手机号也没有有效推广码 =>不处理
					return;
				}
			}else{
				if($exists['status']==0){
					//有手机号但未关联用户
					$data['memberid'] = $params['id'];
					$data['registertime'] = $params['timeadd'];
					$data['status'] = 1;
					if($preaduserid){
						//有有效推广码 => 更新推广员userid
						$data['userid'] = $preaduserid;
					}
					$F = $Model->where(array('id'=>$exists['id']))->save($data);
					return ($F===false) ? false : true;

				}else{
					//有手机号也已关联用户(手动更改手机号后可出现此种情况)=>更新老记录（memberid）的mobile
					$oldmemberinfo = M("{$this->JDDB}member")->where(array('id'=>$exists['memberid']))->find();
					if($oldmemberinfo){
						$F = $Model->where(array('id'=>$exists['id']))->save(array('mobile'=>$oldmemberinfo['mobile']));
					}
					if($preaduserid){
						//又有效推广码=>添加一条新纪录
						$addarr = array(
							'userid' => $preaduserid,
							'memberid' => $params['id'],
							'name'		=> '推广用户@'.$preadcode,
							'mobile'	=> $params['mobile'],
							'timeadd'	=> $params['timeadd'],
							'registertime' => $params['timeadd'],
							'status'	=> 1,
						);
						$res = $Model->add($addarr);
						return ($res && $f)?true:fasle;
					}
				}
			}
    }

}
?>